package com.example.beikeyigou;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.beikeyigou.login.UserBean;
import com.example.beikeyigou.untils.BaseActivity;
import com.example.beikeyigou.untils.SaveUserUtils;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class AnswerActivity extends BaseActivity {

    private EditText oldPwdEt,newPwdEt,rePwdEt;
    private Button commitBtn;
    private ImageView backIv;
    private UserBean.DataBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);
        initView();
        setListener();

    }

    private void setListener() {
        commitBtn.setOnClickListener(onClickListener);
        backIv.setOnClickListener(onClickListener);
    }

    private void initView() {
        oldPwdEt = findViewById(R.id.change_et_formPwd);
        newPwdEt = findViewById(R.id.change_et_pwd);
        rePwdEt = findViewById(R.id.change_et_repwd);
        commitBtn = findViewById(R.id.change_commit_btn);
        backIv = findViewById(R.id.change_back);
        dataBean = SaveUserUtils.getUserInfo(this);

    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.change_back:
                    finish();
                    break;
                case R.id.change_commit_btn:
                    String oldPwd = oldPwdEt.getText().toString();
                    String newPwd = newPwdEt.getText().toString();
                    String rePwd = rePwdEt.getText().toString();

                    if (TextUtils.isEmpty(oldPwd) || TextUtils.isEmpty(newPwd)|| TextUtils.isEmpty(rePwd)){
                        Toast.makeText(AnswerActivity.this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        int id = dataBean.getId();

                        Map<String,String> map = new HashMap<>();
                        map.put("userId",String.valueOf(id));
                        map.put("oldPwd",oldPwd);
                        map.put("newPwd",newPwd);

                        postNetWork(URLUtils.updatePwd_url,map);

                    }
                    break;

            }
            

        }
    };

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        UserBean userBean = new Gson().fromJson(result,UserBean.class);
        int code = userBean.getCode();
        switch (code){
            case 200:
                Toast.makeText(AnswerActivity.this,"密码修改成功！",Toast.LENGTH_SHORT).show();
                //清空本地缓存数据
                SaveUserUtils.deleteUserINfo(this);

                //跳转到更改密码的页面
                Intent intent = new Intent(AnswerActivity.this,MainActivity.class);
                startActivity(intent);
                break;
            default:
                Toast.makeText(AnswerActivity.this,"密码修改失败！",Toast.LENGTH_SHORT).show();
                break;

        }
    }
}